from copy import copy
from openpyxl.utils import get_column_letter

def copy_cell_pro(source_cell,target_cell):
    """
    复制单元格
    :param source_cell:
    :param target_cell:
    """
    target_cell.value = source_cell.value  # 值
    target_cell.font = copy(source_cell.font)  # 字体格式
    target_cell.border = copy(source_cell.border)  # 边框
    target_cell.alignment = copy(source_cell.alignment)  # 对齐方式
    target_cell.fill = copy(source_cell.fill)  # 填充颜色
    target_cell.data_type = copy(source_cell.data_type)  # 数据类型
    target_cell.number_format = copy(source_cell.number_format)  # 数据格式
    target_cell.hyperlink = copy(source_cell.hyperlink)

def copy_worksheet_pro(source_worksheet, target_worksheet):
    """
    跨工作簿复制工作表
    :param source_worksheet:
    :param target_worksheet:
    :return:
    """
    """复制单元格数据和样式"""
    for source_rows in source_worksheet.rows:  # 以行的形式迭代
        for source_cell in source_rows:
            target_cell = target_worksheet[source_cell.coordinate]  # 获取新表对应单元格
            copy_cell_pro(source_cell,target_cell)
    """复制合并单元格"""
    for merged_cell in source_worksheet.merged_cells:
        target_worksheet.merge_cells(str(merged_cell))
    """复制行高和列宽"""
    for row_index in range(1, source_worksheet.max_row + 1):
        row_height = source_worksheet.row_dimensions[row_index].height
        target_worksheet.row_dimensions[row_index].height = row_height
    for column_index in range(1, source_worksheet.max_column + 1):
        column_width = source_worksheet.column_dimensions[get_column_letter(column_index)].width
        target_worksheet.column_dimensions[get_column_letter(column_index)].width = column_width